package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CarPageModel;
import com.southwind.common.ResultObj;
import com.southwind.common.StationPageModel;
import com.southwind.entity.Car;
import com.southwind.entity.Station;
import com.southwind.entity.User;
import com.southwind.mapper.StationMapper;
import com.southwind.service.StationService;
import com.southwind.vo.CarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/station")
public class StationController {

    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private StationService stationService;

    @GetMapping("/show")
    public List<Station> show(){
        return this.stationMapper.show();
    }

    @GetMapping("/list")
    public Map list(StationPageModel stationPageModel){
        Page<Station> page = new Page<>(stationPageModel.getPage(), stationPageModel.getLimit());
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(stationPageModel.getName()), "name", stationPageModel.getName());
        Page<Station> resultPage = this.stationService.page(page, queryWrapper);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        map.put("data", resultPage.getRecords());
        return map;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody Station station){
        boolean save = this.stationService.save(station);
        if(save) return ResultObj.STATION_ADD_SUCCESS;
        return ResultObj.STATION_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody Station station){
        boolean updateById = this.stationService.updateById(station);
        if(updateById) return ResultObj.STATION_UPDATE_SUCCESS;
        return ResultObj.STATION_UPDATE_ERROR;
    }

    @DeleteMapping("/delete/{id}")
    public ResultObj delete(@PathVariable("id") Integer id){
        boolean removeById = this.stationService.removeById(id);
        if(removeById) return ResultObj.STATION_DELETE_SUCCESS;
        return ResultObj.STATION_DELETE_ERROR;
    }
}

